﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.WearningCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.WearningCommandHandlers
{
    /// <summary>
    /// 添加种母断奶信息
    /// </summary>
    public class WeaningCreateCommandHandler : IRequestHandler<WeaningCreateCommand, int>
    {
        private readonly IRepository<Weanings> _weaningsRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="weaningsRepository"></param>
        /// <param name="mapper"></param>
        public WeaningCreateCommandHandler(IRepository<Weanings> weaningsRepository, IMapper mapper)
        {
            _weaningsRepository = weaningsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(WeaningCreateCommand request, CancellationToken cancellationToken)
        {
            request.CreateDate = DateTime.Now;
            request.UpdateDate = null;
            request.UpdateBy = null;
            return await _weaningsRepository.CreateAsync(mapper.Map<Weanings>(request));
        }
    }
}
